import http from './config'
import {ResponseType} from './indexType'
import { message } from "antd";

const request ={
    post({ url, data}, config = {}){
        console.log('post----config-----',config)
        let params = data ? data : {};
        let headers = config || {};
        let promise = new Promise((resolve, reject) => {
            http({
                url: url,
                method: "POST",
                data: params,
                headers: headers
            }).then(res => {
                resolve(res);
            }).catch(err => {
                reject(err);
            })
        });
        return promise;
    },
    get({ url, data },config = {}) {
        let params = data ? data : {};
        let headers = config || {};
        let promise = new Promise((resolve, reject) => {
            http({
                url: url,
                method: "GET",
                params: params,
                headers: headers
            }).then(res => {
                resolve(res);
            }).catch(({ res, msg }) => {
                reject({ res, msg });
            })
        });
        return promise;
    },
    // 封装上传文件的POST请求
    uploadFile({url, data, file},config = {}){
        const formData = new FormData();
        formData.append('file', file);
        Object.keys(data).forEach(key => {
            formData.append(key, data[key]);
        });
        let promise = new Promise((resolve, reject) => {
            http({
                url: url,
                method: "POST",
                headers: {
                    'Content-Type': 'multipart/form-data'
                },
                data:formData,
            }).then(res => {
                resolve(res);
            }).catch(({ res, msg }) => {
                reject({ res, msg });
            })
        });
        return promise;
    },
}
export default  request;

